package com.example;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import java.io.File;
import java.io.IOException;

/**
 * 当前程序调用本地相机，返回记录的相片数据
 */
public class MyActivity extends Activity {


    private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
    private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 200;

    private Uri fileUri;

    /**
     * 内存卡路径
     */
    private static String MEDIA_TYPE_IMAGE;

    static {
        MEDIA_TYPE_IMAGE = Environment.getExternalStorageDirectory() + "/JPhotos";
        System.out.println("相片储存路径:" + MEDIA_TYPE_IMAGE);
    }


    /**
     * Called when the activity is first created.
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button camera = (Button) findViewById(R.id.camera);

        camera.setOnClickListener(new CameraOnclickListener());
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // Image captured and saved to fileUri specified in the Intent
                Toast.makeText(this, "Image saved to:\n" +
                        data.getData(), Toast.LENGTH_LONG).show();


                Bundle extras = data.getExtras();
                Bitmap myBitmap = null;
                if (null != extras) {
                    System.out.println("photos isNull:" + (null == extras));
                    myBitmap = (Bitmap) extras.get("data");
                } else {
                    Uri uri = data.getData();
                    if (uri != null) {
                        myBitmap = BitmapFactory.decodeFile(uri.getPath());
                    }
                }

                System.out.println(myBitmap.getRowBytes());

            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the image capture

            } else {
                // Image capture failed, advise user
            }
        }

        if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
            if (resultCode == RESULT_OK) {
                // Video captured and saved to fileUri specified in the Intent
                Toast.makeText(this, "Video saved to:\n" +
                        data.getData(), Toast.LENGTH_LONG).show();
            } else if (resultCode == RESULT_CANCELED) {
                // User cancelled the video capture
            } else {
                // Video capture failed, advise user
            }
        }
    }

    class CameraOnclickListener implements View.OnClickListener {
        @Override
        public void onClick(View v) {

            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);

            // set the image file name
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

            try {
                System.out.println("Create test file " + (testCreateFile("1.txt") ? "" : " not") + "success");
            } catch (IOException e) {
                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
            }

            // start the image capture Intent
//            startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
            startActivityForResult(Intent.createChooser(intent,
                    "Select Picture"), CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
        }
    }

    /**
     * 获取储存路径
     *
     * @param mediaTypeImage
     *
     * @return
     */
    private Uri getOutputMediaFileUri(String mediaTypeImage) {
        return Uri.fromFile(new File(mediaTypeImage));
    }

    boolean testCreateFile(String fileName) throws IOException {
        File file = new File(fileName);
        if (!file.exists()) {

            file.mkdirs();
            file.createNewFile();
        }

        return file.exists();
    }
}
